package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"user_5.24/api/request"
	"user_5.24/internal/dao"
	"user_5.24/internal/model"
)

func CollectAdd(c *gin.Context) {
	var req request.CollectAdd
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	result := dao.Create(&model.Collect{
		UserId:  c.GetUint("userId"),
		GoodsId: req.GoodsId,
	})

	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "收藏失败",
			"data": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "收藏成功",
		"data": true,
	})
}

func CollectUpdate(c *gin.Context) {
	var req request.CollectUpdate
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var collect model.Collect
	dao.GetOneById(req.Id, &collect)
	if collect.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "未找到收藏视频",
			"data": nil,
		})
		return
	}
	result := dao.Delete(&model.Collect{
		Id: req.Id,
	})

	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "取消收藏失败",
			"data": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "取消收藏成功",
		"data": true,
	})
}

func CollectList(c *gin.Context) {
	var req request.CollectList
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var collect []model.Collect
	dao.GetListByFields(&model.Collect{

		UserId: c.GetUint("userId"),
	}, &collect)

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "收藏列表展示成功",
		"data": collect,
	})
}
